﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<CarHelpDesk.Web.Models.LogonOrRegisterModel>" %>
<style type="text/css">
    .register-field
    {
        display: none;
    }
    .login-field
    {
    }
</style>

<input id="logonSuccessful" type="hidden" value='<%=(Model != null) ? Model.LogonOrRegisterSuccessful.ToString().ToLower() : "false" %>' />
<% Html.EnableClientValidation(); %>
<% using (Ajax.BeginForm("LogonOrRegister", "Account", new AjaxOptions()
                   {
                       HttpMethod = "POST",
                       OnSuccess = "onLogOnSuccess",
                       OnFailure = "onLogOnFailure",
                       OnBegin = "onLogOnSubmitBegin",
                       OnComplete = "onLogOnSubmitComplete"
                   }, new { id = "logon_form" }))
   {%>
<%= Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.") %>
<div>
    <fieldset>
        <legend>Account Information</legend>
        <div class="editor-label">
            <%= Html.LabelFor(m => m.UserName) %>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(m => m.UserName)%>
            <%= Html.ValidationMessageFor(m => m.UserName)%>
        </div>
        <div class="editor-field">
            <%=Html.RadioButtonFor(m => m.LogonOrRegisterAction, "logon", new { @checked = "checked", onClick = "ActionChnaged(this.value)" })%>login&nbsp;&nbsp;&nbsp;
            <%=Html.RadioButtonFor(m => m.LogonOrRegisterAction, "register", new { onClick = "ActionChnaged(this.value)" })%>register
        </div>
        <div class="editor-label">
            <%= Html.LabelFor(m => m.Password)%>
        </div>
        <div class="editor-field">
            <%= Html.PasswordFor(m => m.Password)%>
            <%= Html.ValidationMessageFor(m => m.Password)%>
        </div>
        <div class="editor-label register-field">
            <%= Html.LabelFor(m => m.ConfirmPassword)%>
        </div>
        <div class="editor-field register-field">
            <%= Html.PasswordFor(m => m.ConfirmPassword)%>
            <%= Html.ValidationMessageFor(m => m.ConfirmPassword)%>
        </div>
        <div class="editor-label login-field">
            <%= Html.LabelFor(m => m.RememberMe)%>
            <%= Html.CheckBoxFor(m => m.RememberMe)%>
        </div>
        <div class="editor-label register-field">
            <%= Html.LabelFor(m => m.Email)%>
        </div>
        <div class="editor-field register-field">
            <%= Html.TextBoxFor(m => m.Email)%>
            <%= Html.ValidationMessageFor(m => m.Email)%>
        </div>
        <p>
            <input type="submit" id="LogonOrRegisterButton" value="Login" />
        </p>
    </fieldset>
</div>
<% } %>

<script type="text/javascript">
    function ActionChnaged(action) {
        if (action == "register") {
            $(".register-field").show();
            $(".login-field").hide();
            $("#LogonOrRegisterButton").val("Register");
        }
        else {
            $(".register-field").hide();
            $(".login-field").show();
            $("#LogonOrRegisterButton").val("Login");
        }
    }
    ActionChnaged('<%= (Model != null) ? Model.LogonOrRegisterAction : "logon"%>');

    function onLogOnSuccess(content) {

        $(logonDialog).html(content.get_data());
        if ($('#logonSuccessful').val() == 'true') {
            if (logonDialog) {
                $(logonDialog).dialog('destroy');
                $(logonDialog).remove();
            }
            if (userAuthenticated != null) {
                userAuthenticated();
            }
        }
    }

    function onLogOnFailure(content) {
        alert("LogOn error!");
    }

    function onLogOnSubmitBegin(content) {
        $("#submit_logon").attr("disabled", true);
    }

    function onLogOnSubmitComplete(content) {
        $("#submit_logon").attr("disabled", false);
    }
</script>

